package com.hzjy.api.service.impl;

import com.hzjy.api.beans.*;
import com.hzjy.api.mapper.ApiProjectMapper;
import com.hzjy.api.mapper.ApiWorksMapper;
import com.hzjy.api.mapper.HzjyCollectionMapper;
import com.hzjy.common.enums.TradingStatusEnum;
import com.hzjy.common.utils.JSONUtil;
import com.hzjy.common.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏Service业务层处理
 *
 * @author hzjy
 * @date 2020-08-25
 */
@Service
public class HzjyCollectionServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(HzjyCollectionServiceImpl.class);
    @Autowired
    private HzjyCollectionMapper hzjyCollectionMapper;
    @Autowired
    private ApiProductServiceImpl apiProductService;
    @Autowired
    private ApiProjectMapper apiProjectMapper;
    @Autowired
    private ApiWorksMapper apiWorksMapper;
    @Autowired
    private ApiDelistServiceImpl apiDelistServiceImpl;

    /**
     * 查询收藏
     *
     * @param id 收藏ID
     * @return 收藏
     */
    public HzjyCollection selectHzjyCollectionById(Long id) {
        return hzjyCollectionMapper.selectHzjyCollectionById(id);
    }

    /**
     * 查询收藏列表
     *
     * @param hzjyCollection 收藏
     * @return 收藏
     */
    public List<HzjyCollection> selectHzjyCollectionList(HzjyCollection hzjyCollection) {
        return hzjyCollectionMapper.selectHzjyCollectionList(hzjyCollection);
    }

    /**
     * 新增收藏
     *
     * @param hzjyCollection 收藏
     * @return 结果
     */
    public int insertHzjyCollection(HzjyCollection hzjyCollection) {
        return hzjyCollectionMapper.insertHzjyCollection(hzjyCollection);
    }

    /**
     * 修改收藏
     *
     * @param hzjyCollection 收藏
     * @return 结果
     */
    public int updateHzjyCollection(HzjyCollection hzjyCollection) {
        return hzjyCollectionMapper.updateHzjyCollection(hzjyCollection);
    }

    /**
     * 批量删除收藏
     *
     * @param ids 需要删除的收藏ID
     * @return 结果
     */
    public int deleteHzjyCollectionByIds(Long[] ids) {
        return hzjyCollectionMapper.deleteHzjyCollectionByIds(ids);
    }

    /**
     * 删除收藏信息
     *
     * @param id 收藏ID
     * @return 结果
     */
    public int deleteHzjyCollectionById(Long id) {
        return hzjyCollectionMapper.deleteHzjyCollectionById(id);
    }

    /**
     * 查询我的收藏列表
     *
     * @param pd
     * @param currentUserId 当前登录用户
     * @return
     */
    public Map queryMyCollectionsPage(PageData pd, Long currentUserId) {
        Map map = new HashMap();
        List<HzjyCollection> collectionList = hzjyCollectionMapper.queryCollectionList(pd);
        Integer totalRuslt = hzjyCollectionMapper.queryCollectionCount(pd);
        if (!CollectionUtils.isEmpty(collectionList)) {
            for (HzjyCollection collectionData : collectionList) {
                HzjyProduct hzjyProduct = apiProductService.selectProductInfo(Long.valueOf(collectionData.getProductId()));
                hzjyProduct.setMyTradingTypeWork(0);
                if (hzjyProduct.getState() >= TradingStatusEnum.TradingSuccess.getCode()) {
                    HzjyDelist quertDelist = new HzjyDelist();
                    quertDelist.setProductId(hzjyProduct.getProductId());
                    List<HzjyDelist> hzjyDelistList = apiDelistServiceImpl.selectHzjyDelistList(quertDelist);
                    if (!CollectionUtils.isEmpty(hzjyDelistList)) {
                        for (HzjyDelist delist : hzjyDelistList) {
                            if (Long.valueOf(delist.getUserId()) == currentUserId) {
                                //当前作品 是被当前登录用户摘牌的
                                hzjyProduct.setMyTradingTypeWork(2);
                                break;
                            }
                        }
                    }
                } else {
                    //未挂牌成功的作品
                    if (hzjyProduct.getUserId() == currentUserId.intValue()) {
                        hzjyProduct.setMyTradingTypeWork(1);
                    }
                }
                if (hzjyProduct.getTradingType() == 1) {
                    HzjyProject queryProject = new HzjyProject();
                    queryProject.setProductId(hzjyProduct.getProductId().toString());
                    List<HzjyProject> hzjyProjectList = apiProjectMapper.selectHzjyProjectList(queryProject);
                    log.info("查询项目信息." + JSONUtil.list2json(hzjyProjectList));
                    if (!CollectionUtils.isEmpty(hzjyProjectList)) {
                        hzjyProduct.setHzjyProject(hzjyProjectList.get(0));
                    }
                } else if (hzjyProduct.getTradingType() == 0) {
                    HzjyWorks queryWorks = new HzjyWorks();
                    queryWorks.setProductId(hzjyProduct.getProductId());
                    List<HzjyWorks> hzjyWorksList = apiWorksMapper.selectHzjyWorksList(queryWorks);
                    log.info("查询作品信息." + JSONUtil.list2json(hzjyWorksList));
                    if (!CollectionUtils.isEmpty(hzjyWorksList)) {
                        hzjyProduct.setHzjyWorks(hzjyWorksList.get(0));
                    }
                }
                collectionData.setHzjyProduct(hzjyProduct);
            }
        }
        map.put("collectionList", collectionList);
        map.put("totalPage", pd.getPageNum(totalRuslt, pd.getInteger("size")));
        return map;
    }
}
